package chenjunli;

import java.util.*;

public class TwoColorBalls {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //输入红色球号码
        int[] redNumbers = new int[6];
        Set<Integer> redSet = new HashSet<>();
        for (int i = 0; i < redNumbers.length; i++) {
            while (true) {
                System.out.println("请输入第" + (i + 1) + "个红球的号码，范围1~33：");
                int redNum = scanner.nextInt();
                if (redNum >= 1 && redNum <= 33 && !redSet.contains(redNum)) {
                    redNumbers[i] = redNum;
                    redSet.add(redNum);
                    break;
                } else {
                    System.out.println("输入无效，请重新输入：");
                }
            }
        }

        // 输入蓝色球号码
        int blueNumber = 0; // 使用一个整数来存储蓝球号码
        while (true) {
            System.out.println("请输入蓝球的号码，范围1~16：");
            blueNumber = scanner.nextInt(); // 读取用户输入并存储在 blueNumber 中
            if (blueNumber >= 1 && blueNumber <= 16) { // 检查输入的蓝球号码是否在有效范围内
                break; // 如果输入有效，则退出循环
            } else {
                System.out.println("输入无效，请重新输入："); // 如果输入无效，则提示用户重新输入
            }
        }

        //调用并随机生成中奖号码
        int[] winningRedNumbers = generateWinningRedNumbers();
        int winningBlueNumber = generateWinningBlueNumber();



        // 输出中奖号码和用户投注号码
        System.out.println("中奖号码为：");
        int[] winningNumbers = new int[7];
        System.arraycopy(winningRedNumbers, 0, winningNumbers, 0, 6); // 将红球号码复制到新数组
        winningNumbers[6] = winningBlueNumber; // 将蓝球号码添加到数组末尾
        System.out.println(Arrays.toString(winningNumbers));

        System.out.println("您投注号码为：");
        int[] userNumbers = new int[7];
        System.arraycopy(redNumbers, 0, userNumbers, 0, 6); // 将红球号码复制到新数组
        userNumbers[6] = blueNumber; // 将蓝球号码添加到数组末尾
        System.out.println(Arrays.toString(userNumbers));

        //中奖情况

        int redMatchCount = 0;
        for (int redNum : redNumbers) {
            for (int winningRedNum : winningRedNumbers) {
                if (redNum == winningRedNum) {
                    redMatchCount++;
                    break; // 同一个红球号码只计算一次匹配
                }
            }
        }
        int blueMatch = (blueNumber == winningBlueNumber) ? 1 : 0;

        String prizeLevel;
        if (redMatchCount == 6 && blueMatch == 1) {
            prizeLevel = "一等奖";
        } else if (redMatchCount == 6) {
            prizeLevel = "二等奖";
        } else if (redMatchCount == 5 && blueMatch == 1) {
            prizeLevel = "三等奖";
        } else if (redMatchCount == 5 || (redMatchCount == 4 && blueMatch == 1)) {
            prizeLevel = "四等奖";
        } else if (redMatchCount == 4 || (redMatchCount == 3 && blueMatch == 1)) {
            prizeLevel = "五等奖";
        } else if (redMatchCount == 2 && blueMatch == 1|| redMatchCount == 1 && blueMatch == 1 || blueMatch == 1) {
            prizeLevel = "六等奖";
        } else {
            prizeLevel = "未中奖";
        }

        // 输出结果
        System.out.println("您命中了 " + (redMatchCount + blueMatch) + " 个号码，中奖级别为：" + prizeLevel);

        scanner.close();
    }



    //红色中奖号码
    private static int[] generateWinningRedNumbers() {
        Random random = new Random();
        Set<Integer> redrandom= new HashSet<>();
        while (redrandom.size() < 6) {
            redrandom.add(random.nextInt(33) + 1);
        }
        Integer[] redArray = redrandom.toArray(new Integer[0]);
        int[] result = new int[6];
        for (int i = 0; i < 6; i++) {
            result[i] = redArray[i];
        }
        return result;
    }

    // 随机生成1个蓝球号码
    private static int generateWinningBlueNumber() {
        Random random = new Random();
        return random.nextInt(16) + 1;
    }
}